using System.Text;
using TestCases.CompareAssert.Core.Interfaces;
using TestCases.Utils;

namespace TestCases.CompareAssert.Core.Formatters
{
    public class ResultFormatter : IResultFormatter
    {
        private static readonly ResultFormatter _instance = new ResultFormatter();

// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
// ReSharper disable EmptyConstructor
        static ResultFormatter()
// ReSharper restore EmptyConstructor
        {
        }

        private ResultFormatter()
        {
        }

        public static ResultFormatter Instance
        {
            get { return _instance; }
        }

        public string FormatResult(ICompareResult result)
        {
            var builder = new StringBuilder();
            AppendResult(builder, result, 0);
            return builder.ToString();
        }

        private static void AppendResult(StringBuilder builder, ICompareResult result, int indent)
        {
            builder.AppendLine(new string('\t', indent) + "{Status} => {Comparer}: {Message}".
                FormatWith(new {result.Status, result.Message, Comparer = result.Comparer.Name}));
            foreach (ICompareResult child in result.ChildResults)
            {
                AppendResult(builder, child, indent + 1);
            }
        }
    }
}